//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "ATTraining\fn_clearTargets.sqf"
#include <core_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

if (GVAR(CLEARING_BUSY)) exitWith {TRACE("Clearing already busy");};
GVAR(CLEARING_BUSY) = true;

if (isServer) then {
	private ["_all_units","_units"];
	_all_units = [];
	for [{_i = 0},{_i < GVAR(TRAINING_DISTANCES)},{_i = _i + 1}] do {
		{
			_units = _x;
			{ if (!isnull _x) then { _all_units set [count _all_units, _x]; };			
			} foreach _units;
		} foreach [(GVAR(TRAINING_TARGETS_LEFT) select _i),(GVAR(TRAINING_TARGETS_RIGHT) select _i),(GVAR(TRAINING_TARGETS_MOVING) select _i)];
		GVAR(TRAINING_TARGETS_LEFT) set [_i, []];
		GVAR(TRAINING_TARGETS_RIGHT) set [_i, []];
		GVAR(TRAINING_TARGETS_MOVING) set [_i, []];
	};
	{
		if (typename _x == typename grpNull) then {
			deleteGroup _x;
		} else {
			_x removeAllEventHandlers "HandleDamage";
			deleteVehicle _x;
		};
	} foreach _all_units;
} else {
	for [{_i = 0},{_i < GVAR(TRAINING_DISTANCES)},{_i = _i + 1}] do {
		GVAR(TRAINING_TARGETS_LEFT) set [_i, []];
		GVAR(TRAINING_TARGETS_RIGHT) set [_i, []];
		GVAR(TRAINING_TARGETS_MOVING) set [_i, []];
	};
};
GVAR(CLEARING_BUSY) = false;

TRACE("End");
TMFPROFILERSTOP;
